import { brand } from '../mutation-types'
import * as api from 'api/brand'
const state = {
  recBrandsList: [],
  allBrandList: [],
  seriesList: [],
  brandList: []
}

const actions = {
  getRecBrands ({ commit }) {
    api.getRecBrands().then((data) => {
      commit(brand.GETRECBRANDSLIST, data)
    })
  },
  getAllBrands ({ commit }) {
    api.getAllBrands().then((data) => {
      commit(brand.GETALLBRANDS, data)
    })
  },
  getListSeriesByBrand ({ commit }, parmas) {
    api.getListSeriesByBrand(parmas).then((data) => {
      commit(brand.GETSERIESLIST, data)
    })
  },
  getListTruckByBrand ({ commit }, parmas) {
    return new Promise((resolve, reject) => {
      api.getListTruckByBrand(parmas).then((data) => {
        resolve(data[0])
      }).catch((err) => {
        reject(err)
      })
    })
  }
}

const mutations = {
  [brand.GETRECBRANDSLIST] (state, data) {
    state.recBrandsList = data.slice(0, 8)
  },
  [brand.GETALLBRANDS] (state, data) {
    state.allBrandList = data
  },
  [brand.GETSERIESLIST] (state, data) {
    state.seriesList = data
  }
}

const getters = {
  recBrandsList: (state) => state.recBrandsList,
  allBrandList: (state) => state.allBrandList,
  seriesList: (state) => state.seriesList,
  isSeriesListEmpty: (state) => {
    var flag = true
    state.seriesList.forEach((item, index) => {
      if (item.infos.length > 0) {
        flag = false
      }
    })
    return flag
  }
}

export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations
}
